package com.demo.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

/**
 * 
 * @author 李文彬
 *
 */

public class JSONReadUtil {
	/**
	 * 根据Json文件路径，读取Json文件内容
	 * 
	 * @param path Json文件路径
	 * @return Json文件内容
	 */
	public static String readFile(String path) {
		File file = new File(path);
		BufferedReader reader = null;
		String laststr = "";
		try {
			// System.out.println("以行为单位读取文件内容，一次读一整行：");
			reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
			String tempString = null;
			int line = 1;
			// 一次读入一行，直到读入null为文件结束
			while ((tempString = reader.readLine()) != null) {
				// 显示行号
				// System.out.println("line " + line + ": " + tempString);
				laststr = laststr + tempString;
				line++;
			}
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
		}
		return laststr;
	}

	/**
	 * 解析Json字符串，获取机柜信息
	 * 
	 * @param json Json内容,leiXing 要读取的设备类型
	 * @return 机柜信息的List集合
	 */
	public static List<String> readJiGui(Object json) {
		List<String> listRack = new ArrayList<String>();
		if (json == null) {
			return null;
		}
		try {
			JSONObject jsonObj = JSONObject.fromObject(json);
			Iterator it = jsonObj.keys();
			while (it.hasNext()) {
				String key = (String) it.next();
				Object value = jsonObj.get(key);
				JSONObject jo = JSONObject.fromObject(value);
				if ("机柜".equals(jo.get("CLSID"))) {
					listRack.add(key);
				}
			}
		} catch (Exception e) {
		}
		return listRack;
	}

	/**
	 * 获取动环设备编号
	 * 
	 * @param json Json内容,dongHuan 要读取的动环设备类型
	 * @return 机柜信息的List集合
	 */
	public static List<String> readDongHuan(Object json, String dongHuan) {
		List<String> listRack = new ArrayList<String>();
		if (json == null) {
			return null;
		}
		try {
			JSONObject jsonObj = JSONObject.fromObject(json);
			Iterator it = jsonObj.keys();
			while (it.hasNext()) {
				String key = (String) it.next();
				Object value = jsonObj.get(key);
				JSONObject jo = JSONObject.fromObject(value);
				if (dongHuan.equals(jo.get("标签"))) {
					listRack.add(key);
				}
			}
		} catch (Exception e) {
			System.err.println();
		}
		return listRack;
	}

	/**
	 * 获取动环设备编号
	 * 
	 * @param json Json内容,dongHuan 要读取的动环设备类型
	 * @return 机柜信息的List集合
	 */
	public static List<String> readDongHuan(String path, String dongHuan, List<String> fangJian) {
		List<String> listRack = new ArrayList<String>();
		Object json = JSONReadUtil.readFile(path);
		if (json == null) {
			return null;
		}
		try {
			JSONObject jsonObj = JSONObject.fromObject(json);
			Iterator it = jsonObj.keys();
			while (it.hasNext()) {
				String key = (String) it.next();
				Object value = jsonObj.get(key);
				JSONObject jo = JSONObject.fromObject(value);
				if (dongHuan.equals(jo.get("标签"))) {
					for (int i = 0; i < fangJian.size(); i++) {
						if (fangJian.get(i).equals(jo.get("_PARENT_"))) {
							listRack.add(key);
						}
					}
				}
			}
		} catch (Exception e) {

		}
		return listRack;
	}

	/**
	 * 获取动环设备编号
	 * 
	 * @param json Json内容,dongHuan 要读取的动环设备类型
	 * @return 机柜信息的List集合
	 */
	public static List<String> readDongHuan(String path, String dongHuan) {
		return JSONReadUtil.readDongHuan((Object) JSONReadUtil.readFile(path), dongHuan);
	}

	/**
	 * 将json写入文件
	 */
	public boolean writeFile(String data, String filePath) {
		try {
			File file = new File(filePath);

			if (!file.exists()) {
				file.createNewFile();
			}

			FileWriter fileWritter = new FileWriter(file);
			BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
			bufferWritter.write(data);
			bufferWritter.close();
			return true;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}
}
